package com.yanqu.road.server.manger.unioncampwar;

import com.yanqu.road.entity.unioncampwar.patrons.UnionCampWarLockPatronsData;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class UnionPatronsModel {
    private String unionId;
    /**
     * key1:userId, key2:patronsId 用replace insert into更新数据
     */
    private Map<Long, Map<Integer, UnionCampWarLockPatronsData>> memberPatronsMap = new ConcurrentHashMap<>();

    public UnionPatronsModel(String unionId) {
        this.unionId = unionId;
    }

    public String getUnionId() {
        return unionId;
    }

    public void addPatronsData(UnionCampWarLockPatronsData patronsData) {
        Map<Integer, UnionCampWarLockPatronsData> dataMap = memberPatronsMap.get(patronsData.getUserId());
        if (dataMap == null) {
            synchronized (memberPatronsMap) {
                dataMap = memberPatronsMap.get(patronsData.getUserId());
                if (dataMap == null) {
                    dataMap = new ConcurrentHashMap<>();
                    memberPatronsMap.put(patronsData.getUserId(), dataMap);
                }
            }
        }
        dataMap.put(patronsData.getPatronsId(), patronsData);
    }

    public boolean isNeedSyncPatrons(long userId, long thisWeekId) {
        Map<Integer, UnionCampWarLockPatronsData> dataMap = memberPatronsMap.get(userId);
        if (null == dataMap) {
            return true;
        }

        for (UnionCampWarLockPatronsData lockPatronsData : dataMap.values()) {
            if (lockPatronsData.getUpdateTime() != thisWeekId) {
                return true;
            }
        }
        return false;
    }

    public int getAllPatronsNum() {
        int patronsNum = 0;
        for (Long userId : memberPatronsMap.keySet()) {
            Map<Integer, UnionCampWarLockPatronsData> dataMap = memberPatronsMap.get(userId);
            for (UnionCampWarLockPatronsData lockPatronsData : dataMap.values()) {
                if (lockPatronsData.getPatronsId()  == 0){
                    continue;
                }
                patronsNum++;
            }
        }
        return patronsNum;
    }

    public List<UnionCampWarLockPatronsData> getAllPatronsList() {
        List<UnionCampWarLockPatronsData> list = new ArrayList<>();
        for (Map<Integer, UnionCampWarLockPatronsData> dataMap : memberPatronsMap.values()) {
            for (UnionCampWarLockPatronsData lockPatronsData : dataMap.values()) {
                if (lockPatronsData.getPatronsId() == 0) {
                    continue;
                }
                list.add(lockPatronsData);
            }
        }

        return list;
    }

    public List<UnionCampWarLockPatronsData> getAllSavePatronsList() {
        List<UnionCampWarLockPatronsData> list = new ArrayList<>();
        for (Map<Integer, UnionCampWarLockPatronsData> dataMap : memberPatronsMap.values()) {
            list.addAll(dataMap.values());
        }

        return list;
    }

    public List<UnionCampWarLockPatronsData> getRandomPatronsList() {
        List<UnionCampWarLockPatronsData> list = getAllPatronsList();
        Collections.shuffle(list);

        return list;
    }
}
